class Master:
    """师傅类"""

    def __init__(self):
        self.kongfu = '[古法煎饼果子配方]'

    def make_cake(self):
        print(f'使用{self.kongfu}制作煎饼果子')


class School:
    """学校类"""

    def __init__(self):
        self.kongfu = '[黑马煎饼果子配方]'

    def make_cake(self):
        print(f'使用{self.kongfu}制作煎饼果子')


class Prentice(School, Master):
    """徒弟类"""
    __age = 55

    def __init__(self):
        super().__init__()
        # 定义私有属性
        self.__money = 2000000
        self.kongfu = '[独创煎饼果子配方]'

    @classmethod
    def get_age(cls):
        return cls.__age

    @classmethod
    def set_age(cls, value):
        cls.__age = value

    @property
    def money(self):
        return self.__money

    @money.setter
    def money(self, value):
        self.__money = value

    # 定义私有方法
    def __info_print(self):
        print(self.kongfu)
        print(self.__money)

    def make_cake(self):
        self.__init__()
        print(f'使用{self.kongfu}制作煎饼果子')

    def make_old_cake(self):
        super().__init__()
        super().make_cake()


class TuSun(Prentice):
    """徒孙类"""

    # def __getattr__(self, item):
    #     print('执行__getattr__')
    #     return item

    def __setattr__(self, key, value):
        print('执行__setattr__')
        self.__dict__[key] = value

    def __delattr__(self, item):
        print('执行__delattr__')
        self.__dict__.pop(item)

    def __enter__(self):
        print('执行__enter__')

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('执行__exit__')


if __name__ == '__main__':
    xiaoqiu = TuSun()
    print(xiaoqiu.money)
    xiaoqiu.money = 200
    print(xiaoqiu.money)

    print(xiaoqiu.__dict__)
    print(xiaoqiu.get_age())
    xiaoqiu.set_age(60)
    print(xiaoqiu.get_age())

    print(hasattr(xiaoqiu, 'kongfu'))
    print(getattr(xiaoqiu, 'kongfu'))
    print(hasattr(xiaoqiu, 'money'))
    print(getattr(xiaoqiu, 'money'))

    xiaoqiu.z = 2
    print(xiaoqiu.__dict__)
    # print(xiaoqiu.__age)  # 只有在属性不存在的时候，会自动触发__getattr__
    print(getattr(xiaoqiu, '_age', None))

    del xiaoqiu.z
    print(xiaoqiu.__dict__)

    # 上下文管理器
    with TuSun() as f:
        pass
